package com.lemzdo.community.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lemzdo.community.dto.NoticeDTO;
import com.lemzdo.community.model.Post;
import com.lemzdo.community.model.User;
import com.lemzdo.community.provider.PageInfoProvider;
import com.lemzdo.community.service.NoticeService;
import com.lemzdo.community.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//跳转我的个人资料页面 根据点击按钮不同 进入不同的页面（我的帖子，我的资料等）
@Controller
public class ProfileController {

    @Autowired
    private PostService postService;

    @Autowired//注入手写的pageHelper工具
    private PageInfoProvider pageInfoProvider;

    @Autowired
    private NoticeService noticeService;

    @GetMapping(value = "/profile/{action}")
    //@PathVariable("action") 接受前端传过来的参数
    public String profile(Model model, @PathVariable("action") String action,
                          HttpServletRequest request,@RequestParam(defaultValue = "1" ) Integer pageNum){

        //判断是否登陆
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/";
        }

        //分页显示 我的帖子
        PageHelper.startPage(pageNum,4);
        //从post中查询到对应作者id的帖子，返回到前端
        List<Post> postList = postService.selectAllByAuthorId(user.getId());
        PageInfo<Post> postPageInfo = new PageInfo<>(postList);

        Integer postCount = postService.countByAuthorId(user.getId());
        //手动配置pageHelper
        pageInfoProvider.pageInfoConfig(pageNum,postCount,4,postPageInfo);

        //分页显示我的通知
        PageHelper.startPage(pageNum,4);
        //查询我的通知（user的id作为收到通知人的id）
        List<NoticeDTO> noticeDTOS = noticeService.getAllByReceiverId(user.getId());
        PageInfo<NoticeDTO> noticePageInfo = new PageInfo<>(noticeDTOS);
        Integer noticeCount = noticeService.countByReceiverId(user.getId());
        //手动配置pageHelper
        pageInfoProvider.pageInfoConfig(pageNum,noticeCount,4,noticePageInfo);


        if("posts".equals(action)){//参数是posts，即进入我的帖子页面
            //将要显示的页面信息传到前端
            model.addAttribute("profileTitle","我的帖子");
            model.addAttribute("profileAction","posts");
            model.addAttribute("posts",postList);
            model.addAttribute("postCount",postCount);
            model.addAttribute("postPageInfo",postPageInfo);
        }
        if("notice".equals(action)){//参数是notice，即进入回复我的页面
            //将要显示的页面信息传到前端
            model.addAttribute("profileTitle","我的通知");
            model.addAttribute("profileAction","notice");
            model.addAttribute("noticeDTOS",noticeDTOS);
            model.addAttribute("noticeCount",noticeCount);
            model.addAttribute("noticePageInfo",noticePageInfo);
        }


        return "profile";
    }

}
